<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录 - 物联网数据平台</title>
    
    <!-- Tailwind CSS -->
    <script src="https://cdn.tailwindcss.com"></script>
    
    <!-- FontAwesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- 自定义样式 -->
    <style>
        .login-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .glass-effect {
            background: rgba(255, 255, 255, 0.1);
            backdrop-filter: blur(10px);
            border: 1px solid rgba(255, 255, 255, 0.2);
        }
        
        .fade-in {
            animation: fadeIn 0.6s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .shake {
            animation: shake 0.5s ease-in-out;
        }
        
        @keyframes shake {
            0%, 100% { transform: translateX(0); }
            25% { transform: translateX(-5px); }
            75% { transform: translateX(5px); }
        }
        
        .btn-primary {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .btn-primary:hover {
            background: linear-gradient(135deg, #5a6fd8 0%, #6a4190 100%);
        }
    </style>
</head>
<body class="min-h-screen login-bg flex items-center justify-center p-4">
    <div class="w-full max-w-md">
        <!-- 登录卡片 -->
        <div class="glass-effect rounded-2xl p-8 shadow-2xl fade-in">
            <!-- Logo和标题 -->
            <div class="text-center mb-8">
                <div class="w-20 h-20 bg-white bg-opacity-20 rounded-full flex items-center justify-center mx-auto mb-4">
                    <i class="fas fa-microchip text-white text-3xl"></i>
                </div>
                <h1 class="text-3xl font-bold text-white mb-2">IoT Platform</h1>
                <p class="text-white text-opacity-80">智能监控管理系统</p>
            </div>
            
            <!-- 登录表单 -->
            <form id="loginForm" class="space-y-6">
                <!-- 用户名 -->
                <div>
                    <label for="username" class="block text-sm font-medium text-white text-opacity-90 mb-2">
                        <i class="fas fa-user mr-2"></i>用户名
                    </label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-user text-white text-opacity-60"></i>
                        </div>
                        <input type="text" id="username" name="username" required
                               class="w-full pl-10 pr-3 py-3 border border-white border-opacity-30 rounded-lg bg-white bg-opacity-20 text-white placeholder-white placeholder-opacity-60 focus:outline-none focus:ring-2 focus:ring-white focus:ring-opacity-50 focus:border-white transition-all"
                               placeholder="请输入用户名">
                    </div>
                </div>
                
                <!-- 密码 -->
                <div>
                    <label for="password" class="block text-sm font-medium text-white text-opacity-90 mb-2">
                        <i class="fas fa-lock mr-2"></i>密码
                    </label>
                    <div class="relative">
                        <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
                            <i class="fas fa-lock text-white text-opacity-60"></i>
                        </div>
                        <input type="password" id="password" name="password" required
                               class="w-full pl-10 pr-10 py-3 border border-white border-opacity-30 rounded-lg bg-white bg-opacity-20 text-white placeholder-white placeholder-opacity-60 focus:outline-none focus:ring-2 focus:ring-white focus:ring-opacity-50 focus:border-white transition-all"
                               placeholder="请输入密码">
                        <button type="button" onclick="togglePassword()" class="absolute inset-y-0 right-0 pr-3 flex items-center">
                            <i id="passwordIcon" class="fas fa-eye text-white text-opacity-60 hover:text-opacity-80 transition-colors"></i>
                        </button>
                    </div>
                </div>
                
                <!-- 记住我和忘记密码 -->
                <div class="flex items-center justify-between">
                    <label class="flex items-center">
                        <input type="checkbox" id="rememberMe" class="w-4 h-4 text-blue-600 bg-white bg-opacity-20 border-white border-opacity-30 rounded focus:ring-blue-500 focus:ring-2">
                        <span class="ml-2 text-sm text-white text-opacity-80">记住我</span>
                    </label>
                    <a href="#" class="text-sm text-white text-opacity-80 hover:text-white transition-colors">忘记密码？</a>
                </div>
                
                <!-- 登录按钮 -->
                <button type="submit" class="w-full btn-primary text-white font-medium py-3 px-4 rounded-lg hover:shadow-lg transform hover:scale-105 transition-all duration-200 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-white">
                    <i class="fas fa-sign-in-alt mr-2"></i>
                    登录
                </button>
            </form>
            
            <!-- 其他登录方式 -->
            <div class="mt-6">
                <div class="relative">
                    <div class="absolute inset-0 flex items-center">
                        <div class="w-full border-t border-white border-opacity-30"></div>
                    </div>
                    <div class="relative flex justify-center text-sm">
                        <span class="px-2 bg-transparent text-white text-opacity-60">其他登录方式</span>
                    </div>
                </div>
                
                <div class="mt-4 flex justify-center space-x-4">
                    <button class="w-10 h-10 bg-white bg-opacity-20 rounded-full flex items-center justify-center text-white hover:bg-opacity-30 transition-all">
                        <i class="fab fa-weixin text-lg"></i>
                    </button>
                    <button class="w-10 h-10 bg-white bg-opacity-20 rounded-full flex items-center justify-center text-white hover:bg-opacity-30 transition-all">
                        <i class="fab fa-qq text-lg"></i>
                    </button>
                    <button class="w-10 h-10 bg-white bg-opacity-20 rounded-full flex items-center justify-center text-white hover:bg-opacity-30 transition-all">
                        <i class="fas fa-mobile-alt text-lg"></i>
                    </button>
                </div>
            </div>
            
            <!-- 版权信息 -->
            <div class="mt-8 text-center">
                <p class="text-xs text-white text-opacity-60">
                    © 2024 IoT Platform. All rights reserved.
                </p>
            </div>
        </div>
        
        <!-- 错误提示 -->
        <div id="errorMessage" class="mt-4 bg-red-500 bg-opacity-20 border border-red-500 border-opacity-30 rounded-lg p-4 text-white text-center hidden">
            <i class="fas fa-exclamation-triangle mr-2"></i>
            <span id="errorText"></span>
        </div>
    </div>

    <!-- JavaScript -->
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <script>
        // 切换密码显示/隐藏
        function togglePassword() {
            const passwordInput = document.getElementById('password');
            const passwordIcon = document.getElementById('passwordIcon');
            
            if (passwordInput.type === 'password') {
                passwordInput.type = 'text';
                passwordIcon.classList.remove('fa-eye');
                passwordIcon.classList.add('fa-eye-slash');
            } else {
                passwordInput.type = 'password';
                passwordIcon.classList.remove('fa-eye-slash');
                passwordIcon.classList.add('fa-eye');
            }
        }
        
        // 显示错误信息
        function showError(message) {
            const errorDiv = document.getElementById('errorMessage');
            const errorText = document.getElementById('errorText');
            errorText.textContent = message;
            errorDiv.classList.remove('hidden');
            errorDiv.classList.add('shake');
            
            // 3秒后自动隐藏
            setTimeout(() => {
                errorDiv.classList.add('hidden');
                errorDiv.classList.remove('shake');
            }, 3000);
        }
        
        // 隐藏错误信息
        function hideError() {
            const errorDiv = document.getElementById('errorMessage');
            errorDiv.classList.add('hidden');
        }
        
        // 表单提交处理
        document.getElementById('loginForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const username = document.getElementById('username').value.trim();
            const password = document.getElementById('password').value.trim();
            const rememberMe = document.getElementById('rememberMe').checked;
            
            // 验证输入
            if (!username) {
                showError('请输入用户名');
                document.getElementById('username').focus();
                return;
            }
            
            if (!password) {
                showError('请输入密码');
                document.getElementById('password').focus();
                return;
            }
            
            // 隐藏之前的错误信息
            hideError();
            
            // 显示加载状态
            const submitBtn = e.target.querySelector('button[type="submit"]');
            const originalText = submitBtn.innerHTML;
            submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin mr-2"></i>登录中...';
            submitBtn.disabled = true;
            
            // 发送登录请求
            $.ajax({
                url: '/api/auth/login',
                method: 'POST',
                contentType: 'application/json',
                data: JSON.stringify({
                    username: username,
                    password: password,
                    rememberMe: rememberMe
                }),
                success: function(response) {
                    if (response.code === 200) {
                        // 登录成功，跳转到主页
                        window.location.href = '/';
                    } else {
                        showError(response.message || '登录失败');
                    }
                },
                error: function(xhr) {
                    if (xhr.status === 401) {
                        showError('用户名或密码错误');
                    } else if (xhr.status === 403) {
                        showError('账户已被禁用');
                    } else {
                        showError('网络错误，请稍后重试');
                    }
                },
                complete: function() {
                    // 恢复按钮状态
                    submitBtn.innerHTML = originalText;
                    submitBtn.disabled = false;
                }
            });
        });
        
        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            // 检查是否已经登录
            $.ajax({
                url: '/api/auth/check',
                method: 'GET',
                success: function(response) {
                    if (response.code === 200 && response.data.loggedIn) {
                        // 已经登录，跳转到主页
                        window.location.href = '/';
                    }
                }
            });
            
            // 自动聚焦到用户名输入框
            document.getElementById('username').focus();
            
            // 回车键快速登录
            document.addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    document.getElementById('loginForm').dispatchEvent(new Event('submit'));
                }
            });
        });
    </script>
</body>
</html>
